package com.zhiyou.service.impl;

import com.zhiyou.bean.Comment;
import com.zhiyou.bean.Product;
import com.zhiyou.bean.Type;
import com.zhiyou.bean.User;
import com.zhiyou.dao.CommentMapper;
import com.zhiyou.dao.ProductMapper;
import com.zhiyou.dao.TypeMapper;
import com.zhiyou.dao.UserMapper;
import com.zhiyou.service.ProductService;

import com.zhiyou.service.TypeService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;


@Service
public class ProductServiceImpl implements ProductService{

    @Resource
    ProductMapper productMapper;

    @Resource
    TypeMapper typeMapper;

    @Resource
    CommentMapper commentMapper;

    @Resource
    UserMapper userMapper;

    @Override
    public List<Product> getAllProduct() {
        return productMapper.selectAll();
    }

    @Override
    public List<Product> getProductsByStatus(Integer status) {
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", status);
        return productMapper.selectByExample(example);
    }

    @Override
    public List<Product> getProductsByOrder(String value) {
        if(value.equals("1")){
            Example example= new Example(Product.class);
            example.setOrderByClause("getmoney desc");
            List<Product> products=productMapper.selectByExample(example);
            return products;
        }else if(value.equals("2")){
            Example example= new Example(Product.class);
            example.setOrderByClause("getpeople desc");
            List<Product> products=productMapper.selectByExample(example);
            return products;
        }else {
            Example example= new Example(Product.class);
            example.setOrderByClause("start_time desc");
            List<Product> products=productMapper.selectByExample(example);
            return products;
        }
    }


    @Override
    public Product getProductById(Integer productId) {
        return productMapper.selectByPrimaryKey(productId);
    }

    @Override
    public int insertOne(Product product) {
        return productMapper.insertSelective(product);
    }

    @Override
    public Integer getProductId(Product product) {
        Product p1 = productMapper.selectOne(product);
        return p1.getId();
    }

    @Override
    public int addComment(Comment comment1) {
        return commentMapper.insertSelective(comment1);
    }

    public List<Product> getProductByUserId(Integer id) {
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",id);
        return productMapper.selectByExample(example);
    }

    @Override
    public List<Comment> getAllCommentById(Integer id) {
        Example example = new Example(Comment.class);

        Example.Criteria criteria = example.createCriteria();

        criteria.andEqualTo("productId",id);

        return commentMapper.selectByExample(example);
    }

    public List<Product> getProductByTypeIdJY(int i) {
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("typeId", i);
        return productMapper.selectByExample(example);
    }

    @Override
    public List<Product> getProductByTypeIdAX(int i) {
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("typeId", i);
        return productMapper.selectByExample(example);
    }

    @Override
    public List<Product> getProductByTypeIdFP(int i) {
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("typeId", i);
        return productMapper.selectByExample(example);
    }

    @Override
    public List<Product> getProductByTypeIdCY(int i) {
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("typeId", i);
        return productMapper.selectByExample(example);
    }

    @Override
    public List<Product> getProductByTypeIdHD(int i) {
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("typeId", i);
        return productMapper.selectByExample(example);
    }

    @Override
    public int deleteUserAndProductById(Integer id) {
        return productMapper.deleteUserAndProductById(id);
    }

    @Override
    public List<Product> getProductsBySuccessStatus(int i) {
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", i);
        return productMapper.selectByExample(example);
    }

    @Override
    public User getUserById(Integer userId) {
        return userMapper.selectByPrimaryKey(userId);
    }
}
